package com.custom.util

import io.reactivex.Observable
import io.reactivex.subjects.ReplaySubject

object RxBus {

    //线程安全的 无限制的 重播主题
    val replaySubject = ReplaySubject.create<Any>().toSerialized()

    //返回一个被观察对象
    inline fun <reified T : Any> observe(): Observable<T> {
        return replaySubject.ofType(T::class.java)
    }

    //发送通知
    fun send(event: Any) {
        replaySubject.onNext(event)
    }

    //<地址，包含订阅对象的资源>
//    private val obj_disposable_map: HashMap<Any, CompositeDisposable?> = HashMap()
//
//
//    fun unRegister(disposable: Any) {
//        val compositeDisposable = obj_disposable_map[disposable]
//        if (compositeDisposable == null) {
//            Log.w(TAG, "Trying to unregister subscriber that wasn't registered")
//        } else {
//            compositeDisposable.clear()
//            obj_disposable_map.remove(disposable)
//        }
//    }

//    internal fun register(obj: Any, disposable: Disposable) {
//        var compositeDisposable = obj_disposable_map[obj]
//        if (compositeDisposable == null) {
//            compositeDisposable = CompositeDisposable()
//        }
//        compositeDisposable.add(disposable)
//        obj_disposable_map[obj] = compositeDisposable
//    }
}

/**
 * 注册订阅以便以后正确注销它以避免内存泄漏
 */
//fun Disposable.registerInBus(obj: Any) {
//    RxBus.register(obj, this)
//}
//注册
//RxBus.observe<RefreshRukuOrderBus>().subscribe
// {
// run { mPresenter.sendMsg("") }
//}.registerInBus(this)
//调用模式
//RxBus.send(RefreshRukuOrderBus( "刷新入库单列表"))